﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.NetworkArray`1
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#nullable disable
namespace Netick.Unity;

/// <summary>A networked array.</summary>
[HideInInspector]
[Serializable]
public class NetworkArray<T> : NetworkArray, INetworkCollection, IEnumerable<T>, IEnumerable where T : unmanaged
{
  [SerializeField]
  private T[] _originalBuffer;
  private int _counter;

  public NetworkArray(int capacity)
  {
    this._length = capacity;
    this._originalBuffer = new T[capacity];
    this._elementSizeWords = NetickUtils.GetWordSize(this._elementSizeWords);
  }

  public override unsafe void InternalInit(
    INetickNetworkScript beh,
    int* state,
    int elementSizeWords,
    int flags)
  {
    base.InternalInit(beh, state, elementSizeWords, flags);
    this._elementSizeWords = NetickUtils.GetWordSize(sizeof (T));
  }

  public IEnumerator<T> GetEnumerator()
  {
    NetworkArray<T> networkArray = this;
    for (int i = 0; i < networkArray._length; ++i)
      yield return networkArray[i];
  }

  IEnumerator IEnumerable.GetEnumerator() => (IEnumerator) this.GetEnumerator();

  public void Add(T element)
  {
    this._originalBuffer[this._counter] = element;
    ++this._counter;
  }

  public override void InternalReset()
  {
    int num = Mathf.Min(this._length, this._originalBuffer.Length);
    for (int i = 0; i < num; ++i)
      this[i] = this._originalBuffer[i];
  }

  internal override string PrintValue(int index) => this[index].ToString();

  public unsafe T this[int i]
  {
    get
    {
      if ((IntPtr) this._state == IntPtr.Zero)
        return this._originalBuffer[i];
      if (i < 0 || i > this._length - 1)
        throw new IndexOutOfRangeException();
      return *(T*) (this._state + i * this._elementSizeWords);
    }
    set
    {
      if ((IntPtr) this._state == IntPtr.Zero)
      {
        this._originalBuffer[i] = value;
      }
      else
      {
        if (i < 0 || i > this._length - 1)
          throw new IndexOutOfRangeException();
        this._entity.SetState_Collection((int*) &value, this._state + i * this._elementSizeWords, this._elementSizeWords, (this._flags & NetworkCollectionMeta.HasOnChanged) == NetworkCollectionMeta.HasOnChanged ? 1 : 0, (this._flags & NetworkCollectionMeta.IsInputSourceOnly) == NetworkCollectionMeta.IsInputSourceOnly ? 1 : 0, (this._flags & NetworkCollectionMeta.HasOnChanged) == NetworkCollectionMeta.HasOnChanged ? 1 : 0);
      }
    }
  }
}
